import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 015. 字符串中的所有变位词
 * @author: WXY
 * @create: 2022-12-22 16:15
 * @Version 1.0
 **/

public class offer_five2_findAnagrams {

    //s1中所有的s2的变位词
    public static List<Integer> findAnagrams(String s1, String s2) {
        List<Integer> list = new ArrayList<>();
        int n = s1.length(),m = s2.length();
        if (m > n) return list;
        int[] cnt1 = new int[26];
        int[] cnt2 = new int[26];
        for (int i = 0; i < m; i++) {
            cnt2[s2.charAt(i) - 'a']++;
            cnt1[s1.charAt(i) - 'a']++;
        }
        if (Arrays.equals(cnt1, cnt2)) {
            list.add(0);
        }
        for (int i = m; i < n; i++) {
            cnt1[s1.charAt(i) - 'a']++;
            cnt1[s1.charAt(i - m) - 'a']--;
            if (Arrays.equals(cnt1, cnt2)) {
                list.add(i - m + 1);
            }
        }
        return list;
    }

    public static void main(String[] args) {

    }

    public static boolean checkInclusion(String s1, String s2) {
        int n = s1.length(), m = s2.length();
        if (n > m) {
            return false;
        }
        int[] cnt1 = new int[26];
        int[] cnt2 = new int[26];
        for (int i = 0; i < n; i++) {
            cnt1[s1.charAt(i) - 'a']++;
            cnt2[s2.charAt(i) - 'a']++;
        }
        if (Arrays.equals(cnt1, cnt2)) {
            return true;
        }
        for (int i = n; i < m; i++) {
            cnt2[s2.charAt(i) - 'a']++;
            cnt2[s2.charAt(i - n) - 'a']--;
            if (Arrays.equals(cnt1, cnt2)) {
                return true;
            }
        }
        return false;
    }
}
